package com.java.redis.test;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author jingjintao
 * @date 2021/9/4 - 21:52
 **/
public class JedisPoolUtil {

    private JedisPoolUtil(){

    }
    //被volatile修饰的变量不会被本地线程缓存，对该变量的读写都是直接操作共享内存。
    private static volatile JedisPool jedisPool = null;

    public static JedisPool getJedisPoolInstance(){
        if(null == jedisPool){
            synchronized (JedisPoolUtil.class){
                if(null == jedisPool){
                    JedisPoolConfig poolCOnfig = new JedisPoolConfig();
                    poolCOnfig.setMaxActive(1000);
                    poolCOnfig.setMaxIdle(32);
                    poolCOnfig.setMaxWait(100 * 1000);
                    poolCOnfig.setTestOnBorrow(true);

                    jedisPool = new JedisPool(poolCOnfig, "192.168.10.215", 6379);
                }
            }
        }
        return jedisPool;
    }

    public static void release(JedisPool jedisPool, Jedis jedis){
        if(null != jedis){
            jedisPool.returnResourceObject(jedis);
        }
    }
}
